package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.annotation.Arithmetic;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarRotationOver;
import com.navinfo.platform.trip.common.arithmetic.enums.EventType;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.config.ConfigMap;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 引擎超转速相关算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计、事件<br/>
 */
@Arithmetic(name = "引擎超转速算法")
public class RotationOver extends AbstractArithmetic<VarRotationOver> {
    /**
     * 事件类型
     */
    private final EventType EVENT_TYPE = EventType.engineOverSpeed;
    /**
     * 引擎超转速阀值
     */
    private final Integer rotationThreshold = ConfigMap.getInteger("E_Rev_Threshold");
    /**
     * 引擎超转速判定
     */
    private boolean isOverRotation(){
        return getCommonData().getRotation() > rotationThreshold;
    }

    @Override
    public void calculate() {
        logger.debug("===============引擎超转速算法开始===========");
        VarRotationOver var = getObj();
        boolean preState= var.isPreOver();
        boolean curState = isOverRotation();
        logger.debug("进入引擎超转速算法，前一点状态：{}，当前点状态：{}，[terminalId:{}]", preState, curState, terminalId);

        switch (getEventStatus(preState, curState)) {
            case START:
                logger.debug("引擎超转速算法，事件开始.[terminalId:{}]", terminalId);
                startEventData(EVENT_TYPE);
                break;
            case IN:
                logger.debug("引擎超转速算法，事件中.[terminalId:{}]", terminalId);
                calcEventData(EVENT_TYPE);
                break;
            case END:
                logger.debug("引擎超转速算法，事件结束.[terminalId:{}]", terminalId);
                calcEventData(EVENT_TYPE);
                endEventData();
                break;
            default:
                logger.debug("跳出引擎超转速算法.[terminalId:{}]", terminalId);
        }

        //行程结束时要清理对应的未结束事件
        if (getCommonData().isTripLastNode()) {
            endEventData();
        }

        //更新状态，供下个点使用
        var.setPreOver(curState);
        logger.debug("===============引擎超转速算法结束===========");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRotationOver varRotationOver = getObj();
        statisticDataOut.setSrMileage(ArithmeticUtils.mileageMToM(varRotationOver.getMileage()));
        statisticDataOut.setSrFuel(ArithmeticUtils.fuelMlToMl(varRotationOver.getFuel()));
        statisticDataOut.setSrDuration(ArithmeticUtils.timeMsToSecond(varRotationOver.getDuration()));
        statisticDataOut.setSrNumber(varRotationOver.getNumber());
    }

    private void endEventData(){
        endEventData(EVENT_TYPE, eventData -> {
            VarRotationOver var = getObj();
            var.addNumber( 1);
            var.addMileage( Optional.ofNullable(eventData.getExtendField05()).orElse(0.0));
            var.addFuel( Optional.ofNullable(eventData.getExtendField06()).orElse(0.0).intValue());
            var.addDuration( Optional.ofNullable(eventData.getDuration()).orElse(0L).intValue());
        });
    }
}
